import type { CommandModule } from '@/types/cli'
import type { OptionValues, Command } from 'commander'
import * as password from '@/utils/password'

const command: CommandModule = {
  name: ['encr', 'encrypt'],
  args: ['<password>'],
  description: 'encrypted password',
  before(command) {
    command.option('--compare <encrypted>')
  },
  action(args: [string, OptionValues, Command]) {
    const pwd = args[0]
    const uniqueOpts = args[1]
    if (uniqueOpts.compare) {
      password.verify(pwd, uniqueOpts.compare).then((res) => console.log(String(res)))
    } else {
      password
        .encrypt(pwd)
        .then((encrypted) => {
          console.log(encrypted)
        })
        .catch((err) => {
          console.log(err)
        })
    }
  }
}

export default command
